package com.qkl.xiatang.app.service.impl;

import com.qkl.xiatang.app.cache.LocalCacheBuilder;
import com.qkl.xiatang.app.cache.LocalSimpleCache;
import com.qkl.xiatang.app.entity.sms.SmsDynamicConfig;
import com.qkl.xiatang.app.mapper.SmsDynamicConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
@Slf4j
public class SmsDynamicConfigService {

    @Autowired
    private SmsDynamicConfigMapper dynamicConfigMapper;

    private LocalSimpleCache<SmsDynamicConfig> configCache;

    @PostConstruct
    public void initConfigCache(){
        LocalCacheBuilder<SmsDynamicConfig> builder = new LocalCacheBuilder<>();
        configCache = builder.build(30,null,()-> queryConfig());
    }

    public SmsDynamicConfig getConfig(){
        return configCache.getCache();
    }

    private SmsDynamicConfig queryConfig(){
        List<SmsDynamicConfig> list = dynamicConfigMapper.selectList(null);
        if(ObjectUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

}
